package com.jt.controller;

import com.jt.pojo.Item;
import com.jt.pojo.ItemDesc;
import com.jt.vo.EasyUITable;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jt.service.ItemService;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/item")
public class ItemController {
	
	@Autowired
	private ItemService itemService;

	/**
	 * 业务说明: 根据分页实现商品查询
	 * URL地址:	http://localhost:8091/item/query?page=1&rows=20
	 * 参数:	page/rows
	 * 返回值:  EasyUITable
	 */
	@RequestMapping("/query")
	public EasyUITable findItemByPage(Integer page, Integer rows){

		return itemService.findItemByPage(page,rows);
	}

	/**
	 * 业务说明: 商品新增
	 * 	URL地址: 	http://localhost:8091/item/save
	 * 	请求参数: 	form表单 item对象接收
	 * 	返回值:		SysResult对象
	 */
	@RequestMapping("/save")
	public SysResult saveItem(Item item, ItemDesc itemDesc){
	    itemService.saveItem(item,itemDesc);
	    return SysResult.success();
    }

	/**
	 * 完成商品修改操作
	 * url地址:	http://localhost:8091/item/update
	 * 参数:    form表单
	 * 返回值:  SysResult对象
	 */
    @RequestMapping("/update")
    public SysResult updateItem(Item item,ItemDesc itemDesc){
		itemService.updateItem(item,itemDesc);
		return SysResult.success();
	}

	/**
	 * 批量删除商品
	 * url地址:	http://localhost:8091/item/delete
	 * 参数:    ids=100,101,102
	 * 返回值:  SysResult对象
	 * 简化操作: 当参数采用,号分隔时,会自动的转化为数组.
	 * SpringMVC 底层实现servlet
	 */
	@RequestMapping("/delete")
	public SysResult deleteItems(Long...ids){
		itemService.deleteItem(ids);
		return SysResult.success();
	}

	@RequestMapping("/updateStatus/{status}")
	public SysResult updateStatus(@PathVariable Integer status,Long...ids){
		itemService.updateStatus(status,ids);
		return SysResult.success();
	}

	@RequestMapping("/query/item/desc/{id}")
	public SysResult findItemDescById(@PathVariable Long id){
		ItemDesc itemDesc = itemService.findItemDescById(id);
		return SysResult.success(itemDesc);
	}

}
